local julian = fk.CreateSkill {
  name = "lingling__julian",
}

Fk:loadTranslationTable{
  ["lingling__julian"] = "巨敛",
  [":lingling__julian"] = "你攻击范围内的其他角色回合结束时，若其总牌数不少于三张，你可以弃置一张牌令其选择：交给你两张牌并随机使用一张装备牌；交给你一张装备牌并随机弃置一张牌。",
  ["lingling__julian_active"] = "巨敛",

  ["#lingling__julian-invoke"] = "是否对 %dest 发动 巨敛？",
  ["#lingling__julian-give"] = "巨敛：交给 %dest 两张牌并随机使用一张装备牌；或交给其一张装备牌并随机弃置一张牌",
  ["lingling__julian1"] = "交出两张牌并随机使用一张装备牌",
  ["lingling__julian2"] = "交出一张装备牌并随机弃置一张牌",
}

local U = require "packages/utility/utility"

julian:addEffect(fk.TurnEnd, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(julian.name) and not target.dead and
      #target:getCardIds("he") > 2 and player:inMyAttackRange(target) and target ~= player
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cards = room:askToDiscard(player, {
      skill_name = julian.name,
      prompt = "#lingling__julian-invoke::"..target.id,
      max_num = 1,
      min_num = 1,
      pattern = ".",
      skip = true,
    })
    if #cards > 0 then
      event:setCostData(self,{ cards = cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:throwCard(event:getCostData(self).cards,julian.name,player)
    local cards, choice = U.askForCardByMultiPatterns(
      target,
      {
        { ".", 2, 2, "lingling__julian1" },
        { ".|.|.|.|.|equip", 1, 1, "lingling__julian2" }
      },
      julian.name,
      false,
      "#lingling__julian-give::" .. player.id
    )
    room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonGive, julian.name, nil, false, target)
    if #cards == 2 then
      local equips = {}
      for _, id in ipairs(room.draw_pile) do
        local card = Fk:getCardById(id)
        if card.type == Card.TypeEquip and player:canUseTo(card, player) then
          table.insert(equips, card)
        end
      end
      if #cards > 0 then
        room:useCard({
          from = target,
          tos = {target},
          card = table.random(equips),
        })
        return true
      end
    else
      local card = table.random(target:getCardIds("he"))
      room:throwCard(card,julian.name,target)
    end
  end,
})

return julian
